package com.clss.emergencycall.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 唐浩 on 2018/3/29.
 */

public class TimeUtils {
    private static final String TAG = "TimeUtils";

    //
    public static long getTime(){
        Calendar calendar = Calendar.getInstance();
        long time = calendar.getTime().getTime();
        return time;
    }

    public static String getTime(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(date);
    }

    public static String getTime(long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    public static String getFIFTime(long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    public static String getFIFTime(String timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }

    public static String getTime(String timeStamp){
        if (TextUtils.isEmpty(timeStamp)){
            return "";
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }

    public static String getyyyymmddHHmmTime(String timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }

    public static String getAllTime(String timeStamp){
        if (TextUtils.isEmpty(timeStamp)){
            return "";
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }
    public static String getAllTimeShort(String timeStamp){
        if (TextUtils.isEmpty(timeStamp)){
            return "";
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }
    public static long getTimeNum(String mStrBegin, String mStrEnd){
            Long beginT = Long.parseLong(mStrBegin);
            Long endT = Long.parseLong(mStrEnd);
        return  endT - beginT;
    }


    public static String getUpTime(long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    public static String getMunuteTime(String timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }

    public static String getHourTime(long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    public static String getDownTime(long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    public static String getDownTime(String timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(timeStamp)));
        return sd;
    }

    public static String getpublicTime(long time , String yyyyMMddHHmmss){
        SimpleDateFormat sdf=new SimpleDateFormat(yyyyMMddHHmmss);
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    public static List<Integer> getTimeNum(long time){
        int hour = (int) ((time/1000)/60/60);
        int miniue = (int) ((time/1000)/60%60);
        int second = (int) ((time/1000)%60);
        List<Integer> list = new ArrayList<>();
        list.add(hour);
        list.add(miniue);
        list.add(second );
        return list;
    }
    public static String getYYMMddTime(String date) throws ParseException {
        return getTime(dateToStamp(date)+"");
    }

    public static String getMMddTime(String date) throws ParseException {
        return getUpTime(dateToStamp(date));
    }

    public static String getHHmmTime(String data) throws ParseException {
        return getHourTime(dateToStamp(data));
    }
    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
//        long ts = date.getTime();
//        res = String.valueOf(ts);
        return date.getTime();
    }

    public static String getWeekAgoData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateNow = new Date();
        Calendar cl = Calendar.getInstance();
        cl.setTime(dateNow);
        cl.add(Calendar.WEEK_OF_YEAR, -1);
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom);
    }
    public static String timeToDate(String time) {
        try {
            Long aLong = Long.valueOf(time);
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formater.format(aLong);
        } catch (NumberFormatException e) {
            return "";
        }
    }

    public static boolean isEffectiveDate(Long start, Long ending) {
            Date startTime = new Date(start);
            Date endTime = new Date(ending);
            Date nowTime = new Date();
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);

            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);

            Calendar end = Calendar.getInstance();
            end.setTime(endTime);

            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
    }
}
